package tankGame05;

import tankGame05.tank.Bullet;
import tankGame05.tank.Enemy;
import tankGame05.tank.Hero;
import tankGame05.window.MyPanel;

import java.io.*;
import java.util.Vector;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-03-29
 * Time:16:20
 */
@SuppressWarnings("all")
public class Recorder {
    private static int destoryedEnemyNum = 0;
    private static ObjectInputStream ois = null;
    private static ObjectOutputStream oos = null;
    private static final String recordPath = "src/tankGame05/Record.txt";
    private static final String recordPathBackup = "f:/TankBattle/Record.txt";
    private static Hero hero = null;
    private static Vector<Enemy> enemies = null;
    private static Vector<Bullet> enemyMagazine = null;

    public static void recordData() {
        hero = MyPanel.getHero();
        enemies = MyPanel.getEnemies();
        enemyMagazine = MyPanel.getEnemyMagazine();
        try {
            oos = new ObjectOutputStream(new FileOutputStream(recordPath));
            oos.writeInt(destoryedEnemyNum);
            oos.writeObject(hero);
            oos.writeObject(enemies);
            oos.writeObject(enemyMagazine);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        backupRecord();
    }

    private static void backupRecord() {
        try {
            oos = new ObjectOutputStream(new FileOutputStream(recordPathBackup));
            oos.writeInt(destoryedEnemyNum);
            oos.writeObject(hero);
            oos.writeObject(enemies);
            oos.writeObject(enemyMagazine);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                    oos = null;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static void readData() {
        File file = new File(recordPath);
        if (!file.exists()) {
            file = new File(recordPathBackup);
            if (!file.exists()) {
                return;
            }
        }
        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            destoryedEnemyNum = ois.readInt();
            hero = (Hero) ois.readObject();
            enemies = (Vector<Enemy>) ois.readObject();
            enemyMagazine = (Vector<Bullet>) ois.readObject();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                    ois = null;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static int getDestoryedEnemyNum() {
        return destoryedEnemyNum;
    }

    public static void setDestoryedEnemyNum(int destoryedEnemyNum) {
        Recorder.destoryedEnemyNum = destoryedEnemyNum;
    }

    public static Hero getHero() {
        return hero;
    }

    public static Vector<Enemy> getEnemies() {
        return enemies;
    }

    public static Vector<Bullet> getEnemyMagazine() {
        return enemyMagazine;
    }
}
